﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


public enum MoveDirection
{
    UP,
    DOWN,
    RIGHT,
    LEFT
}

// 控制游戏逻辑
public class GameController : MonoBehaviour,IPointerDownHandler, IDragHandler
{

    private GameCore gameCore;//游戏算法核心
    private NumberSprite[,] numberSpriteArray;//地图的图片显示脚本
    private bool isDown = false;//是否按下检测
    private Vector2 startPos;//按下的起始点


    // 初始化操作
    private void Awake()
    {
        gameCore = new GameCore();
        numberSpriteArray = new NumberSprite[4, 4];
        CreateInitMap();
    }

    // 开始游戏的时候默认创建2个数字
    private void Start()
    {
        GenerateNewNumber();
        GenerateNewNumber();
    }

    // 检测格子变化则更新地图
    private void Update()
    {
        if (gameCore.isChange)
        {
            UpdateMap();
            GenerateNewNumber();
            gameCore.isChange = false;
        }
        if (gameCore.IsOver())
        {
            EventMgr.instance.Emit("GameOver", gameCore.score);
        }
    }

    // 创建游戏4*4初始格子
    private void CreateInitMap()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                CreateSprite(i, j);
            }
        }
    }

    // 根据坐标创建精灵格子
    private void CreateSprite(int r, int c)
    {
        GameObject go = new GameObject(string.Format("{0},{1}", r, c));
        go.transform.SetParent(transform, false);
        go.AddComponent<Image>();
        NumberSprite ns = go.AddComponent<NumberSprite>();
        numberSpriteArray[r, c] = ns;
        ns.SetImage(0);
    }

    // 生成新数字
    private void GenerateNewNumber()
    {        
        Location location;
        int number;
        gameCore.GenerateNumber(out location, out number);
        if (number == -1)
            return;
        numberSpriteArray[location.row, location.col].SetImage(number);
        numberSpriteArray[location.row, location.col].CreateEffect();

    }

    // 更新地图
    private void UpdateMap()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                numberSpriteArray[i, j].SetImage(gameCore.Map[i, j]);
            }
        }
    }

    //拖拽操作
    public void OnDrag(PointerEventData eventData)
    {
        if (!isDown)
            return;
        Vector2 offset = eventData.position-startPos;
        float x = Mathf.Abs(offset.x);
        float y = Mathf.Abs(offset.y);
        MoveDirection? dir = null;
        // 左右 大于5像素有效，防止误触
        if (x > y && x > 5)
        {
            dir = offset.x > 0 ? MoveDirection.RIGHT : MoveDirection.LEFT;
        }
        // 上下
        else if (x < y && y > 5)
        {
            dir = offset.y > 0 ? MoveDirection.UP : MoveDirection.DOWN;
        }

        if (dir != null)
        {
            gameCore.Move(dir.Value);
        }
        isDown = false;

    }

    public void OnPointerDown(PointerEventData eventData)
    {
        startPos = eventData.position;
        isDown = true;
    }
}